/*
 * StringParameterValidator.java July 2009
 *
 * Copyright (C) 2009, Quirino Zagarese <quirino@laszloitalia.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 */
package org.eu4ria.parameters;

import java.util.regex.Pattern;

import org.eu4ria.enums.RegexPatterns;
import org.eu4ria.exceptions.IllegalParameterValueException;

/**
 * The Class ParameterValidator.
 */
public class StringParameterValidator extends ParameterValidator{

	/** The parameter class. */
	private Class parameterClass;

	/** The accepted pattern. */
	private RegexPatterns acceptedPattern;

	/** The excepted chars. */
	private String exceptedChars;

	/** The custom pattern. */
	private String customPattern;

	/** The minlength. */
	private int minlength;

	/** The maxlength. */
	private int maxlength;

	/** The compiled pattern. */
	private Pattern compiledPattern;




	/**
	 * Gets the accepted pattern.
	 *
	 * @return the accepted pattern
	 */
	public RegexPatterns getAcceptedPattern() {
		return acceptedPattern;
	}

	/**
	 * Sets the accepted pattern.
	 *
	 * @param acceptedPattern
	 *            the new accepted pattern
	 */
	public void setAcceptedPattern(RegexPatterns acceptedPattern) {
		this.acceptedPattern = acceptedPattern;
	}

	/**
	 * Gets the custom pattern.
	 *
	 * @return the custom pattern
	 */
	public String getCustomPattern() {
		return customPattern;
	}

	/**
	 * Sets the custom pattern.
	 *
	 * @param customPattern
	 *            the new custom pattern
	 */
	public void setCustomPattern(String customPattern) {
		this.customPattern = customPattern;
	}



	/**
	 * Gets the excepted chars.
	 *
	 * @return the excepted chars
	 */
	public String getExceptedChars() {
		return exceptedChars;
	}

	/**
	 * Sets the excepted chars.
	 *
	 * @param exceptedChars
	 *            the new excepted chars
	 */
	public void setExceptedChars(String exceptedChars) {
		this.exceptedChars = exceptedChars;
	}

	/**
	 * Gets the maxlength.
	 *
	 * @return the maxlength
	 */
	public int getMaxlength() {
		return maxlength;
	}

	/**
	 * Sets the maxlength.
	 *
	 * @param maxlength
	 *            the new maxlength
	 */
	public void setMaxlength(int maxlength) {
		this.maxlength = maxlength;
	}

	/**
	 * Gets the minlength.
	 *
	 * @return the minlength
	 */
	public int getMinlength() {
		return minlength;
	}

	/**
	 * Sets the minlength.
	 *
	 * @param minlength
	 *            the new minlength
	 */
	public void setMinlength(int minlength) {
		this.minlength = minlength;
	}

	/**
	 * Gets the parameter class.
	 *
	 * @return the parameter class
	 */
	public Class getParameterClass() {
		return parameterClass;
	}

	/**
	 * Sets the parameter class.
	 *
	 * @param parameterClass
	 *            the new parameter class
	 */
	public void setParameterClass(Class parameterClass) {
		this.parameterClass = parameterClass;
	}

	/**
	 * Compile pattern.
	 */
	public void compilePattern() {
		if (!customPattern.equalsIgnoreCase("")) {
			compiledPattern = Pattern.compile(customPattern);
		} else if(acceptedPattern.equals(RegexPatterns.ANY)){
			compiledPattern = Pattern.compile(".*");
		}else{
			String pattern = "[" + acceptedPattern.getPattern();
			if (!exceptedChars.equalsIgnoreCase("")) {
				pattern = pattern + "[^" + exceptedChars + "]";
			}
			pattern = pattern + "]" + "{" + minlength + "," + maxlength + "}";
			compiledPattern = Pattern.compile(pattern);
		}
	}

	/**
	 * Gets the compiled pattern.
	 *
	 * @return the compiled pattern
	 */
	public Pattern getCompiledPattern() {
		return compiledPattern;
	}

	@Override
	public void acceptsValue(Object value, Class type) throws IllegalParameterValueException {
		String s = (String)value;
		boolean acceptable = compiledPattern.matcher(s).matches();
		if(!acceptable){
			throw new IllegalParameterValueException("Input value does not accomplish specified format.");
		}

	}

	@Override
	public Object getJavaValue(String string) {
		return string;
	}


}
